package com.macro.mall.service.impl;

import com.macro.mall.dto.BmsRecruitmentInformationParam;
import com.macro.mall.dto.BmsRecuitmentInformationDetail;
import com.macro.mall.mapper.BmsRecruitmentInformationMapper;
import com.macro.mall.mapper.BmsRecuitmentInformationPicMapper;
import com.macro.mall.model.BmsRecruitmentInformation;
import com.macro.mall.model.BmsRecuitmentInformationPic;
import com.macro.mall.service.BmsRecruitmentInformationService;
import com.macro.mall.utils.UserUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 观光车点位图片Service实现类
 * Created by macro on 2018/4/26.
 */
@Service
public class BmsRecruitmentInformationServiceImpl implements BmsRecruitmentInformationService {

    @Autowired
    private BmsRecruitmentInformationMapper bmsRecruitmentInformationMapper;
    @Autowired
    private UserUtils userUtils;

    @Autowired
    BmsRecuitmentInformationPicMapper bmsRecuitmentInformationPicMapper;
    @Transactional
    @Override
    public int create(BmsRecruitmentInformationParam bmsRecruitmentInformationParam) {
        int count = 0;
        Long userId = userUtils.getUserId();
        BmsRecruitmentInformation bmsRecruitmentInformation = new BmsRecruitmentInformation();
        BeanUtils.copyProperties(bmsRecruitmentInformationParam, bmsRecruitmentInformation);
        bmsRecruitmentInformation.setCreateUser(userId);
        bmsRecruitmentInformation.setCreateTime(new Date());
        bmsRecruitmentInformation.setUpdateUser(userId);
        bmsRecruitmentInformation.setUpdateTime(new Date());
        if (bmsRecruitmentInformationMapper.insert(bmsRecruitmentInformation)>0){
            //是封面图
            String coverPic = bmsRecruitmentInformationParam.getCoverPic();
            //不是封面图
            List<String> picList = bmsRecruitmentInformationParam.getPicList();

            BmsRecuitmentInformationPic bmsRecruitmentInformationPic;
            if (StringUtils.isNotBlank(coverPic)){
                bmsRecruitmentInformationPic = new BmsRecuitmentInformationPic();
                bmsRecruitmentInformationPic.setPic(coverPic);
                bmsRecruitmentInformationPic.setRecruitmentInformationId(bmsRecruitmentInformation.getId());
                bmsRecruitmentInformationPic.setIsCover(1);
                if (bmsRecuitmentInformationPicMapper.insert(bmsRecruitmentInformationPic) < 0){
                    return -1;
                }
            }
            if (CollectionUtils.isNotEmpty(picList)) {
                for (String pic : picList) {
                    bmsRecruitmentInformationPic = new BmsRecuitmentInformationPic();
                    bmsRecruitmentInformationPic.setPic(pic);
                    bmsRecruitmentInformationPic.setRecruitmentInformationId(bmsRecruitmentInformation.getId());
                    bmsRecruitmentInformationPic.setIsCover(0);
                    if (bmsRecuitmentInformationPicMapper.insert(bmsRecruitmentInformationPic) <0){
                        return -1;
                    }
                }
            }
            count = 1;
            return count;
        }
        return 0;
    }

    /**
     * 获取列表
     * @param bmsRecruitmentInformationParam
     * @return
     */
    @Override
    public List<BmsRecruitmentInformation> getList(BmsRecruitmentInformationParam bmsRecruitmentInformationParam) {
        BmsRecruitmentInformation bmsRecruitmentInformation = new BmsRecruitmentInformation();
        BeanUtils.copyProperties(bmsRecruitmentInformationParam, bmsRecruitmentInformation);
        List<BmsRecruitmentInformation> list = bmsRecruitmentInformationMapper.getList(bmsRecruitmentInformation);
        return list;
    }

    @Override
    public BmsRecruitmentInformation getDetail(Long id) {

        BmsRecruitmentInformation bmsRecruitmentInformation = bmsRecruitmentInformationMapper.selectById(id);
        BmsRecuitmentInformationDetail detail = new BmsRecuitmentInformationDetail();
        BeanUtils.copyProperties(bmsRecruitmentInformation, detail);
        // 获取封面图
        String coverPic = bmsRecruitmentInformationMapper.findCoverPicByActivitiesId(id);
        // 获取活动图片
        List<String> picList = bmsRecruitmentInformationMapper.findPicListByActivitiesId(id);
        detail.setCoverPic(coverPic);
        detail.setPicList(picList);
        return detail;
    }

    @Transactional
    @Override
    public int delete(List<Long> ids) {
        int count = 0;
        if (bmsRecruitmentInformationMapper.delete(ids)>0){
            if (bmsRecuitmentInformationPicMapper.delete(ids) > 0){
                count = 1;
                return count;
            }
        }
        return count;
    }

    /**
     * 修改招募信息
     * @param bmsRecruitmentInformationParam
     * @return
     */
    @Transactional
    @Override
    public int update(BmsRecruitmentInformationParam bmsRecruitmentInformationParam) {
        int count = 1;
        Long userId = userUtils.getUserId();
        BmsRecruitmentInformation bmsRecruitmentInformation = new BmsRecruitmentInformation();
        BeanUtils.copyProperties(bmsRecruitmentInformationParam, bmsRecruitmentInformation);
        bmsRecruitmentInformation.setCreateUser(userId);
        bmsRecruitmentInformation.setCreateTime(new Date());
        bmsRecruitmentInformation.setUpdateUser(userId);
        bmsRecruitmentInformation.setUpdateTime(new Date());
        if ( bmsRecruitmentInformationMapper.update(bmsRecruitmentInformation)>0) {
            List<Long> ids = new ArrayList<>();
            ids.add(bmsRecruitmentInformationParam.getId());
            if (bmsRecuitmentInformationPicMapper.delete(ids) > 0) {
                BmsRecuitmentInformationPic bmsRecruitmentInformationPic;
                //是封面图
                String coverPic = bmsRecruitmentInformationParam.getCoverPic();
                //不是封面图
                List<String> picList = bmsRecruitmentInformationParam.getPicList();
                //新增封面图
                if (StringUtils.isNotBlank(coverPic)) {
                    bmsRecruitmentInformationPic = new BmsRecuitmentInformationPic();
                    bmsRecruitmentInformationPic.setPic(coverPic);
                    bmsRecruitmentInformationPic.setRecruitmentInformationId(bmsRecruitmentInformation.getId());
                    bmsRecruitmentInformationPic.setIsCover(1);
                    if (bmsRecuitmentInformationPicMapper.insert(bmsRecruitmentInformationPic) < 0) {
                        return -1;
                    }
                }
                //新增不是封面图
                if (CollectionUtils.isNotEmpty(picList)) {
                    for (String pic : picList) {
                        bmsRecruitmentInformationPic = new BmsRecuitmentInformationPic();
                        bmsRecruitmentInformationPic.setPic(pic);
                        bmsRecruitmentInformationPic.setRecruitmentInformationId(bmsRecruitmentInformation.getId());
                        bmsRecruitmentInformationPic.setIsCover(0);
                        if (bmsRecuitmentInformationPicMapper.insert(bmsRecruitmentInformationPic) < 0) {
                            return -1;
                        }
                    }
                }
            }
        }else {
            count = 0;
            return count;
        }
        return count;
    }

}
